package com.yitong.base.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utils_代理
 * 
 * @author kuaiditu
 * @version1.0
 */
public class HttpUtils {

	// 发送http请求
	public static String sendHttp(String url) throws Exception {
		// 创建连接
		URL httpurl = new URL(url);
		HttpURLConnection connection = (HttpURLConnection) httpurl.openConnection();
		connection.connect();
		// 获取输入流
		InputStream in = connection.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		StringBuffer sb = new StringBuffer();
		String str = null;
		while ((str = br.readLine()) != null) {
			sb.append(str);
		}
		// 反馈结果
		String content = sb.toString();
		// 关闭输入流
		br.close();
		in.close();
		return content;
	}
	
	public static String replaces(String reg,String content,boolean isfirst){
		
		//匹配字符
		Pattern p = Pattern.compile(reg);
		//替换
		Matcher m = p.matcher(content);
		//判断是否只替换第一个
		String ret = isfirst?m.replaceFirst(""):m.replaceAll("");
		return ret;
	}
	
	public static List<String> getMatch(String reg, String content) {

		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(content);
		List<String> list = new ArrayList<String>();
		while (m.find()) {
			list.add(m.group());
		}
		return list;
	}
}
